package com.itlaobing.web.management.controller;



import com.itlaobing.pojo.SystemUser;
import com.itlaobing.service.SystemUserService;
import com.itlaobing.util.PageUtil;
import com.itlaobing.util.StringHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/management/user")
public class ManagementUserController {
    @Autowired
    private SystemUserService systemUserService;

    private Log log = LogFactory.getLog(ManagementUserController.class);
    @Autowired
    private SystemUserService  systemuserservice;

    private  Log log1=LogFactory.getLog(ManagementUserController.class);


    /**
     * 后台所有用户显示
     * @return
     */
    @RequestMapping(path = "/page/list",method = {RequestMethod.GET,RequestMethod.POST})
    public String userList(Integer pageIndex, String condition, Model model){

        //创建分页实体类对象
        PageUtil<SystemUser> page = new PageUtil<SystemUser>();

        //判断条件
        if(StringHelper.empty(condition)){
            condition="";
        }
        page.setCondition(condition);

        //获取总条数
        int count = systemuserservice.findCountByPage(page);
        page.setRecordCount(count);

        //判断当前页
        if(pageIndex==null){
            pageIndex=1;
        }
        if(pageIndex<=1){
            pageIndex=1;
        }
        if(pageIndex>=page.getPageCount()){
            pageIndex=page.getPageCount();
        }
        page.setPageIndex(pageIndex);

        //设置页码
        page.setPageNums(5);

        //获取分页数据
        List<SystemUser> us = systemuserservice.findUserByPage(page);
        page.setDatas(us);

        model.addAttribute("putil",page);

        return "/management/user/list";
    }

    /**
     * 打开新建账号页面
     * @return
     */
    @RequestMapping(path = "/page/create",method = RequestMethod.GET)
    public String createUser(){
        return  "/management/user/create";
    }

    /**
     * 新建管理员账号
     * @return
     */
    @RequestMapping(path = "/action/save",method = RequestMethod.POST)
    public  String  saveUser(SystemUser user,Model model){
        //数据校验
        if(StringHelper.empty(user.getSystemuseraccount())){
            model.addAttribute("user",user);
            model.addAttribute("error","请填写管理员账号");
            return "/management/user/create";
        }
        if(StringHelper.empty(user.getSystemusername())){
            model.addAttribute("user",user);
            model.addAttribute("error","请填写管理员姓名");
            return "/management/user/create";
        }
        if(StringHelper.empty(user.getSystemrolename())){
            model.addAttribute("user",user);
            model.addAttribute("error","请填写管理员身份");
            return "/management/user/create";
        }
        if(user.getSystemuserstatus()==null){
            model.addAttribute("user",user);
            model.addAttribute("error","请填写管理员状态");
            return "/management/user/create";
        }
        //数据封装
        user.setSystemuserid(StringHelper.uuid());
        user.setSystemuserpassword("12345678");
        user.setSystemuserdatetime(new Date());
        //调用业务
        boolean rs=systemUserService.saveUser(user);
        //结果判断
        if(!rs){
            model.addAttribute("defeat","服务器异常，请联系管理员解决");
            return "/management/user/create";
        }
        return "redirect:/management/user/page/list";
    }

    /**
     * 查看用户信息
     * @return
     */
    @RequestMapping(path = "/page/show/{id}",method = RequestMethod.GET)
    public String show(@PathVariable("id") String id,Model model){
            //调用业务，根据id查询用户信息
        SystemUser user=systemUserService.findOneById(id);
        model.addAttribute("user",user);
            return "/management/user/show";
    }
    //锁定、解锁用户
    @RequestMapping(path = "/action/lock/{id}/{status}",method = RequestMethod.GET)
    public String lock(@PathVariable("id") String id,@PathVariable("status") Integer status,Model model){
        //数据封装
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("id",id);
        map.put("status",status);
        //调用业务,修改用户状态
        boolean rs=systemUserService.updateStausById(map);
        //结果判断
        if(!rs){
            model.addAttribute("defeat","服务器异常，请稍后再试");
            return "/management/user/list";
        }
        return "redirect:/management/user/page/list";
    }
    /**
     * 删除用户信息（物理删除，直接将数据库里的数据给删除掉）
     */
    @RequestMapping(path = "/action/delete/{id}",method = RequestMethod.GET)
    public String delete(@PathVariable("id") String id,Model model){
        //调用业务，删除用户信息
        Boolean rs=systemUserService.deleteById(id);
        if(!rs){
            model.addAttribute("defeat","服务器异常，请稍后");
            return "/management/user/list";
        }
        return "redirect:/management/user/page/list";
    }
}
